package com.yuyue.springFramework.note11;

import com.yuyue.springFramework.note11.entity.code.OrderCreateEvent;
import com.yuyue.springFramework.note11.entity.code.OrderCreateEventListener;
import com.yuyue.springFramework.note11.entity.custom1.MyConfig1;
import com.yuyue.springFramework.note11.entity.custom1.UserRegisterService;
import com.yuyue.springFramework.note11.entity.custom2.MyConfig2;
import com.yuyue.springFramework.note11.entity.custom2.UserRegisterService2;
import com.yuyue.springFramework.note11.entity.custom3.MyConfig3;
import com.yuyue.springFramework.note11.entity.custom3.UserRegisterService3;
import com.yuyue.springFramework.note11.entity.springCode.MyConfig4;
import com.yuyue.springFramework.note11.entity.springCode.UserRegisterService4;
import com.yuyue.springFramework.note11.entity.springCode1.MyConfig5;
import com.yuyue.springFramework.note11.entity.springCode1.UserRegisterService5;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;

/**
 * 《Spring-11 Event事件》文章代码
 *  文章地址：https://mp.weixin.qq.com/s/06-fRthRwjQFJ-IDR9eGpw
 */
public class Note11Test {
    @Test
    //单线程事件手动实现版本,bean定义在Config中
    public void fun1(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //注册配置类，并通过配置类扫描同包下的类
        applicationContext.register(MyConfig1.class);
        applicationContext.refresh();

        //获取用户注册bean
        UserRegisterService userRegisterService = applicationContext.getBean(UserRegisterService.class);
        //用户注册
        userRegisterService.registerUser("鱼跃");
    }

    @Test
    //单线程事件手动实现版本,bean定义在各自类上，进一步解耦
    public void fun2(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //注册配置类，并通过配置类扫描同包下的类
        applicationContext.register(MyConfig2.class);
        applicationContext.refresh();

        //获取用户注册bean
        UserRegisterService2 userRegisterService = applicationContext.getBean(UserRegisterService2.class);
        //用户注册
        userRegisterService.registerUser("鱼跃");
    }

    @Test
    //Spring接口硬编码的方式使用广播
    public void fun3(){
        //创建Spring提供的广播器简单实现
        ApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
        //创建订单监听器并放入广播器中
        eventMulticaster.addApplicationListener(new OrderCreateEventListener());
        //创建事件放入广播器中
        eventMulticaster.multicastEvent(new OrderCreateEvent(this, "313"));
    }

    @Test
    //实现发布器接口的方式获取发布器
    public void fun4(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //注册配置类，并通过配置类扫描同包下的类
        applicationContext.register(MyConfig3.class);
        applicationContext.refresh();

        //获取用户注册bean
        UserRegisterService3 userRegisterService = applicationContext.getBean(UserRegisterService3.class);
        //用户注册
        userRegisterService.registerUser("鱼跃");

    }

    @Test
    //使用注解注册监听器
    public void fun5(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //注册配置类，并通过配置类扫描同包下的类
        applicationContext.register(MyConfig4.class);
        applicationContext.refresh();

        //获取用户注册bean
        UserRegisterService4 userRegisterService = applicationContext.getBean(UserRegisterService4.class);
        //用户注册
        userRegisterService.registerUser("鱼跃");
    }

    @Test
    //异步监听器
    public void fun6(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //注册配置类，并通过配置类扫描同包下的类
        applicationContext.register(MyConfig5.class);
        applicationContext.refresh();

        //获取用户注册bean
        UserRegisterService5 userRegisterService = applicationContext.getBean(UserRegisterService5.class);
        //用户注册
        userRegisterService.registerUser("鱼跃");

        System.out.println(Thread.currentThread());
    }
}
